package com.it.controller.admin;

import com.it.admin.AdminCategoryService;
import com.it.admin.AdminPlateService;
import com.it.bean.R;
import com.it.bean.RestStatusEnum;
import com.it.pojo.Category;
import com.it.pojo.Plate;
import com.it.pojo.params.BasePage;
import com.it.pojo.params.category.CategoryEditQuery;
import com.it.pojo.params.plate.PlateEditQuery;
import com.it.pojo.vo.category.AdminCategoryVO;
import com.it.pojo.vo.plate.AdminPlateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/admin/plate")
@Api(value = "v1", tags = "板块相关接口")
public class AdminPlateController {
    @Resource
    private AdminPlateService adminPlateService;

    @GetMapping("list")
    @ApiOperation(value = "查询所有板块", notes = "查询所有板块")
    public R<?> list() {
        List<AdminPlateVO> adminPlateVOList = adminPlateService.listPlates();
        return R.success("成功", adminPlateVOList);
    }


    @PostMapping("save")
    @ApiOperation(value = "新增板块", notes = "新增板块")
    public R<?> save(@RequestParam("plateName") String plateName) {
        if (StringUtils.isEmpty(plateName)) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        return adminPlateService.insertPlate(plateName) > 0 ? R.success() : R.error();
    }

    @PostMapping("list")
    @ApiOperation(value = "查询板块", notes = "查询板块")
    public R<?> list(@RequestBody BasePage basePage) {
        Integer pageNum = basePage.getPageNum();
        Integer limit = basePage.getLimit();
        if (pageNum <= 0 || limit <= 0) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        Map<String, Object> map = adminPlateService.listPlates(basePage);
        return R.success("成功", map);
    }

    @DeleteMapping("{plateid}")
    @ApiOperation(value = "删除板块", notes = "删除板块")
    public R<?> delete(@PathVariable("plateid") Integer plateId) {
        if (plateId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        return adminPlateService.deletePlateById(plateId)
                > 0 ? R.success() : R.error();
    }

    @PutMapping("")
    @ApiOperation(value = "根据板块id修改板块信息", notes = "根据板块id修改板块信息")
    public R<?> edit(@RequestBody PlateEditQuery plateEditQuery) {
        if (plateEditQuery == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        // TODO 参数校验不做
        Plate plate = new Plate();
        BeanUtils.copyProperties(plateEditQuery, plate);
        return adminPlateService.updatePlateById(plate)
                > 0 ? R.success() : R.error();
    }

    @GetMapping("{plateid}")
    public R<?> getCategory(@PathVariable("plateid") Integer plateId) {
        if (plateId == null) {
            return R.error(RestStatusEnum.PARAMS_ERROR);
        }
        String rest = adminPlateService.getPlate(plateId);
        return R.success("成功", rest);
    }
}
